import { existsSync } from 'fs';
import { writeFile } from 'fs/promises';
import { join } from 'path';

import { FileFactoryBase, IFile } from '../../contract';

export class FsFile implements IFile {
    public path: string;

    public constructor(
        public name: string,
        private m_FileFactory: FileFactoryBase,
    ) {
        this.path = join(this.m_FileFactory.cwd, this.m_FileFactory.view, this.name);
    }
    
    public exists() {
        return existsSync(this.path);
    }

    public async write(text: string) {
        await writeFile(this.path, text, 'utf-8',);
    }
}